package com.cdut.controller;

import com.cdut.pojo.Good;
import com.cdut.service.GoodService;
import com.cdut.util.JsonResponce;
import com.cdut.util.PageSetting;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * author: xinmu
 * date: 2022/4/21
 * time: 9:12
 */

@CrossOrigin
@Controller
@Api
@RequestMapping("/good")
public class GoodController {

    @Autowired
    GoodService goodService;

    /**
     *  添加商品
     * @param good 商品
     * @return 状态码
     */
    @PostMapping("/addgood")
    @ApiOperation("添加商品")
    @ResponseBody
    public int addGood(Good good, @RequestParam("file")MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        Integer uid= Integer.parseInt((request.getParameter("uid")));
        good.setUid(uid);
        int code=goodService.addGood(good,multipartFile).getCode();
        return code;
    }

    /**
     * 删除商品
     * @param gid 商品id
     * @return 状态
     */
    @ApiOperation("删除商品")
    @GetMapping("/deletegood")
    public int deleteGood(int gid){
        int code=goodService.deleteGood(gid).getCode();
        return code;
    }

    /**
     * 查询全部的商品列表
     * @return 全部的商品
     */
    @ApiOperation("查询全部的商品")
    @GetMapping("/queryallgood")
    public List<Good> queryGoodList(){
        JsonResponce<List<Good>> listJsonResponce = goodService.queryGoodList();
        //全部的商品
        List<Good> goodList  = listJsonResponce.getData();
        //状态码
        int code=listJsonResponce.getCode();

        return goodList;

    }

    /**
     * 通过商品名查询商品
     * @param mv w
     * @return 查询到的商品
     */
    @ApiOperation("通过名字查询商品")
    @RequestMapping(value = "querygoodbyname2",method = RequestMethod.GET)
    public String queryGoodByName(Model mv,HttpServletRequest request){
        String name=request.getParameter("name");
        JsonResponce<List<Good>> listJsonResponce = goodService.queryGoodByName(name);
        List<Good> goodList = listJsonResponce.getData();
        mv.addAttribute("goodlist",goodList);
//        int code=listJsonResponce.getCode();
        return "gridproducts_bannerslider2";
    }

    /**
     * 更新商品
     * @param good 商品实例
     * @return 状态码
     */
    @ApiOperation("更新商品的信息")
    @PostMapping("/updategood")
    public int upDateGood(Good good){
        JsonResponce<Integer> jsonResponce = goodService.upDateGood(good);
        int code = jsonResponce.getCode();
        return code;
    }

    /**
     * 通过商品id查询
     * @param gid 商品id
     * @return 状态码
     */
    @ApiOperation("通过商品id查询")
    @GetMapping("/querygoobyid")
    public Good queryGoodById(int gid){
        JsonResponce<Good> goodJsonResponce = goodService.queryGoodById(gid);
        int code = goodJsonResponce.getCode();
        Good good = goodJsonResponce.getData();
        return good;
    }

    /**
     * 通过
     * @param tag 分类的标签
     * @return 查询到的商品
     */
    @ApiOperation("通过标签查询")
    @GetMapping("/querygoodbytag")
    public List<Good> queryGoodByTag(String tag){
        JsonResponce<List<Good>> listJsonResponce = goodService.queryGoodByTag(tag);
        int code=listJsonResponce.getCode();
        List<Good> goodList = listJsonResponce.getData();
        return goodList;
    }


    /**
     * 通过标签和商品名查询
     * @param gname 商品名
     * @param tag 标签
     * @return list
     */
    @ApiOperation("通过标签和商品名查询")
    @GetMapping("/query-good")
    public List<Good> queryGood(String gname,String tag){
        JsonResponce<List<Good>> listJsonResponce = goodService.queryGood(gname, tag);
        int code=listJsonResponce.getCode();
        List<Good> goodList = listJsonResponce.getData();
        return goodList;
    }

    /**
     * 分页查询
     * @param gname 商品名
     * @param tag 标签
     * @param pageNum 页码
     * @return list
     */
    @GetMapping("/query-good-page")
    @ApiOperation("分页查询商品")
    public List<Good> queryGoodPage(String gname, String tag,@RequestParam(defaultValue = "1") int pageNum){

        if (ObjectUtils.isEmpty(pageNum)){
            List<Good> goodList = goodService.queryGoodPages(gname, tag, 1, PageSetting.pageSize);
            return goodList;
        }else{
            List<Good> goodList = goodService.queryGoodPages(gname, tag, pageNum, PageSetting.pageSize);
            return goodList;
        }
    }

    /**
     * 分页查询全部商品
     * @param pageNum 页面
     * @return 商品单
     */
    @GetMapping("/all-good-page")
    @ApiOperation("查询全部的商品页")
    public List<Good> queryGoodListPage(@RequestParam(defaultValue = "1") int pageNum){
        return goodService.queryGoodListPage(pageNum,PageSetting.storePageSize);
    }

    /**
     * 标签分页查询
     * @param tag 标签
     * @param pageNum 页码
     * @return list
     */
    @GetMapping("/tag-good-list")
    @ApiOperation("标签页查询")
    public List<Good> queryTagListPage(String tag,@RequestParam(defaultValue = "1") int pageNum){
        List<Good> goodList = goodService.queryGoodTagPage(tag, pageNum, PageSetting.storePageSize);
        return goodList;
    }

    /**
     * 搜索用户的商品
     * @param uid 用户id
     * @return list
     */
    @ApiOperation("搜索用户的商品")
    @GetMapping("/user-good")
    @ResponseBody
    public List<Good> queryGoodByUid(int uid){

        List<Good> goodList = goodService.queryGoodByUid(uid);

        return goodList;
    }

}
